package com.pcbly.web.components.email;
 
import java.security.Security;
import java.util.Properties; 

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName; 
import javax.mail.internet.MimeUtility;  

public abstract class AbstractReceive extends MailReceiveFrom { 
	
	protected Store getStore() throws Exception {
		String mailhost = bean.getPopHostName();
		Properties props = System.getProperties(); 
		if (bean.getPopSSL() != null && bean.getPopSSL().equals("1")) {
			Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
			props.setProperty("mail.pop3.socketFactory.class",
					"javax.net.ssl.SSLSocketFactory");
			props.setProperty("mail.pop3.socketFactory.fallback",
					"false");
			props.setProperty("mail.pop3.socketFactory.port", ""
					+ bean.getPopPort());
		}else
		props.setProperty("mail.pop3.port", ""+bean.getPopPort());

		URLName urln = new URLName("pop3", mailhost, bean.getPopPort(), null,
				getFromEmail(), bean.getPassword());
		Session session = Session.getDefaultInstance(props, null);
		Store store = session.getStore(urln);
		store.connect();
		return store;
	}
	

	
	

	protected String getSubject(Message message) throws MessagingException {
		String subject = "";
		try {
			System.out.println("ContentType ::::::::::::::::"
					+ message.getContentType());
			log
					.debug("ContentType ::::::::::::::::"
							+ message.getContentType());
			String fileName = message.getContentType();
			subject = message.getSubject();
			if (subject != null) {
				if (fileName != null) {
					if (fileName.toLowerCase().indexOf("gb2312") != -1) {
						subject = MimeUtility.decodeText(subject);
					}
				} else {
					subject = MimeUtility.decodeText(subject);
				}
			}
			if (subject == null)
				subject = "";
			System.out.println(subject);
			System.out.println("subject ::::::::::::::::" + subject);
			log.debug("subject ::::::::::::::::" + subject);
		} catch (Exception exce) {
			exce.printStackTrace();
		}
		return subject;
	} 

	protected String getMailContent(Part part) throws Exception {
		String contenttype = part.getContentType();
		int nameindex = contenttype.indexOf("name");
		boolean conname = false;
		if (nameindex != -1)
			conname = true;
		if (part.isMimeType("text/plain") && !conname) {
			return MimeUtility.decodeText((String) part.getContent());
		} else if (part.isMimeType("text/html") && !conname) {
			return MimeUtility.decodeText((String) part.getContent());
		} else if (part.isMimeType("multipart/*")) {
			StringBuilder sb = new StringBuilder();
			Multipart multipart = (Multipart) part.getContent();
			int counts = multipart.getCount();
			for (int i = 0; i < counts; i++) {
				sb.append(getMailContent(multipart.getBodyPart(i)));
			}
			return MimeUtility.decodeText(sb.toString());
		} else if (part.isMimeType("message/rfc822")) {
			return MimeUtility.decodeText((String) getMailContent((Part) part
					.getContent()));
		}
		return null;
	}

}
